home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / FormProcessor.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  6.1 KB  |  173 lines

  1. package horst;
  2.  
  3. import java.awt.Component;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.io.DataInputStream;
  9. import java.io.DataOutputStream;
  10. import java.io.FilterOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStreamReader;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15. import java.net.URLEncoder;
  16. import java.util.Enumeration;
  17. import java.util.EventObject;
  18. import java.util.Hashtable;
  19. import java.util.Vector;
  20. import javax.swing.DefaultComboBoxModel;
  21. import javax.swing.JButton;
  22. import javax.swing.JComboBox;
  23. import javax.swing.JTextField;
  24.  
  25. public class FormProcessor implements ActionListener, KeyListener {
  26.    Vector m_comps = new Vector();
  27.    Vector m_submits = new Vector();
  28.    Hashtable m_atts;
  29.    HTMLPane m_renderer;
  30.  
  31.    public FormProcessor(HTMLPane renderer, Hashtable atts) {
  32.       this.m_renderer = renderer;
  33.       this.m_atts = atts;
  34.    }
  35.  
  36.    public void actionPerformed(ActionEvent e) {
  37.       Object src = ((EventObject)e).getSource();
  38.       if (src instanceof JButton) {
  39.          JButton btn = (JButton)src;
  40.          String method = (String)this.m_atts.get("method");
  41.          String action = (String)this.m_atts.get("action");
  42.          if (action != null) {
  43.             String formString = this.getFormString();
  44.             if (method != null && method.equalsIgnoreCase("POST")) {
  45.                URL u = Utilities.getURL(action);
  46.                if (u != null) {
  47.                   try {
  48.                      URLConnection conn = u.openConnection();
  49.                      conn.setDoInput(true);
  50.                      conn.setDoOutput(true);
  51.                      conn.setUseCaches(false);
  52.                      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  53.                      DataOutputStream printout = new DataOutputStream(conn.getOutputStream());
  54.                      printout.writeBytes(formString);
  55.                      ((FilterOutputStream)printout).close();
  56.                      DataInputStream input = new DataInputStream(conn.getInputStream());
  57.                      InputStreamReader reader = new InputStreamReader(input);
  58.                      this.m_renderer.openPage(reader, u);
  59.                   } catch (IOException var12) {
  60.                   }
  61.                }
  62.             } else {
  63.                formString = "?" + formString;
  64.                URL u = Utilities.getURL(action + formString);
  65.                if (u == null && this.m_renderer != null) {
  66.                   u = Utilities.getURL(this.m_renderer.getDocument().getBaseURL(), action + formString);
  67.                }
  68.  
  69.                this.m_renderer.openPage(u);
  70.             }
  71.          }
  72.       }
  73.  
  74.    }
  75.  
  76.    void addComponent(Element e) {
  77.       this.m_comps.addElement(e);
  78.       Component c = (Component)e.getAttribute("component");
  79.       String type = (String)e.getAttribute("type");
  80.       if (type != null && (type.equalsIgnoreCase("submit") || type.equalsIgnoreCase("image")) && c != null && c instanceof JButton) {
  81.          this.m_submits.addElement(c);
  82.          ((JButton)c).addActionListener(this);
  83.       }
  84.  
  85.       if (c instanceof JTextField) {
  86.          ((JTextField)c).addKeyListener(this);
  87.       }
  88.  
  89.    }
  90.  
  91.    Hashtable getComponentAttributes(Component c) {
  92.       Enumeration e = this.m_comps.elements();
  93.  
  94.       while(e.hasMoreElements()) {
  95.          Element e1 = (Element)e.nextElement();
  96.          Component c1 = (Component)e1.getAttribute("component");
  97.          if (c1 == c) {
  98.             return e1.getAttributes();
  99.          }
  100.       }
  101.  
  102.       return null;
  103.    }
  104.  
  105.    public Vector getComponents() {
  106.       return this.m_comps;
  107.    }
  108.  
  109.    public Hashtable getFormAttributes() {
  110.       return this.m_atts;
  111.    }
  112.  
  113.    String getFormString() {
  114.       String str = "";
  115.       int nCount = 0;
  116.       Enumeration e = this.m_comps.elements();
  117.  
  118.       while(e.hasMoreElements()) {
  119.          Element e1 = (Element)e.nextElement();
  120.          Component c1 = (Component)e1.getAttribute("component");
  121.          if (c1 == null || !this.m_submits.contains(c1)) {
  122.             String type = (String)e1.getAttribute("type");
  123.             String name = (String)e1.getAttribute("name");
  124.             String value = "";
  125.             if (c1 != null && c1 instanceof JTextField) {
  126.                value = ((JTextField)c1).getText();
  127.             } else if (c1 instanceof JComboBox) {
  128.                DefaultComboBoxModel model = (DefaultComboBoxModel)((JComboBox)c1).getModel();
  129.                ComboBoxItem item = (ComboBoxItem)model.getSelectedItem();
  130.                if (item != null) {
  131.                   value = (String)item.m_atts.get("value");
  132.                }
  133.             } else {
  134.                value = (String)e1.getAttribute("value");
  135.             }
  136.  
  137.             if (value != null && name != null) {
  138.                value = URLEncoder.encode(value);
  139.                if (nCount++ == 0) {
  140.                   str = str + name + "=" + value;
  141.                } else {
  142.                   str = str + "&" + name + "=" + value;
  143.                }
  144.             }
  145.          }
  146.       }
  147.  
  148.       return str;
  149.    }
  150.  
  151.    public void keyPressed(KeyEvent e) {
  152.    }
  153.  
  154.    public void keyReleased(KeyEvent e) {
  155.       if (e.getKeyCode() == 10) {
  156.          String action = (String)this.m_atts.get("action");
  157.          if (action != null) {
  158.             String formString = this.getFormString();
  159.             URL u = Utilities.getURL(action + formString);
  160.             if (u == null && this.m_renderer != null) {
  161.                u = Utilities.getURL(this.m_renderer.getDocument().getBaseURL(), action + formString);
  162.             }
  163.  
  164.             this.m_renderer.openPage(u);
  165.          }
  166.       }
  167.  
  168.    }
  169.  
  170.    public void keyTyped(KeyEvent e) {
  171.    }
  172. }
  173.